//订单
var product = {
	id:"",
	course:null,
	carData:"",
};

var sc=new Array();
//订单详情
product.detail = function(){
	var args = getQueryStringArgs();
	product.id = args["id"];
	var ajax = new $ax(Feng.ctxPath+"/order/getById", function(data){
        Feng.log("查询详情成功");
        if (data.success){
			 //图片过滤器
		    template.helper('pictureFormat', function (inp) {
		       if(inp){
		           return Feng.ctxPath+"/file/getImage/"+inp;
		       }
		       else{
		        	return "themes/img/pd/course_defult.jpg";
		       }
		    });
		    
		    //价格过滤器
            template.helper('coursePriceFormat', function (inp) {
                if(inp!==""){
                    return numeral(inp).format('0,0.00');
                }
            });
		    
			
			for(var i=0;i<data.obj.orderItems.length;i++)
		    {  
		        sc.push(data.obj.orderItems[i].course);
		    }
			console.log(data);
         
            //课程基础信息
            //价格过滤器
//          template.helper('coursePriceFormat', function (inp) {
//              if(inp!==""){
//                  return numeral(inp).format('0,0.00');
//              }
//          });
//			alert(data.obj.userName);
			var outHtml = template("detailTmpl",data.obj);
			$(".aui-myOrder-content").after(outHtml);
    
       
        }
    },function(data){
        Feng.error("查询详情失败!");
    });
    ajax.set("id",product.id);
    ajax.start();
}


//初始微信支付信息
product.initPay = function(){
	var url=window.location.href;	
	console.log("回传地址："+url);
	var ajax = new $ax(Feng.ctxPath+"/jsConfig", function(data){
        console.log("初始化微信支付完成");
        console.log(data);
               wx.config({
		            debug: false,
		            appId: data.appId, // 必填，公众号的唯一标识
		            timestamp: data.timestamp, // 必填，生成签名的时间戳
		            nonceStr: data.nonceStr, // 必填，生成签名的随机串
		            signature: data.signature,// 必填，签名，见附录1
		            jsApiList: [
		                'checkJsApi',
		                'onMenuShareTimeline',
		                'onMenuShareAppMessage',
		                'onMenuShareQQ',
		                'onMenuShareWeibo',
		                'onMenuShareQZone',
		                'hideMenuItems',
		                'showMenuItems',
		                'hideAllNonBaseMenuItem',
		                'showAllNonBaseMenuItem',
		                'translateVoice',
		                'startRecord',
		                'stopRecord',
		                'onVoiceRecordEnd',
		                'playVoice',
		                'onVoicePlayEnd',
		                'pauseVoice',
		                'stopVoice',
		                'uploadVoice',
		                'downloadVoice',
		                'chooseImage',
		                'previewImage',
		                'uploadImage',
		                'downloadImage',
		                'getNetworkType',
		                'openLocation',
		                'getLocation',
		                'hideOptionMenu',
		                'showOptionMenu',
		                'closeWindow',
		                'scanQRCode',
		                'chooseWXPay',
		                'openProductSpecificView',
		                'addCard',
		                'chooseCard',
		                'openCard',
		                'getBrandWCPayRequest'
		            ]
		        });
		    
    },function(data){
    	console.log("初始化微信支付失败");
    	console.log(data);        
    });
    ajax.type = "get";
    ajax.set({url:url});
    ajax.start();
}


//微信支付 发送订单ID给微信支付
product.wecatPay = function(orderId){
	console.log("支付前："+Feng.userId);
	if(orderId){	    
	    var ajax = new $ax(Feng.ctxPath+"/pay/"+orderId, function(data){
	    	console.log("微信支付成功");
	        console.log(data);
	         WeixinJSBridge.invoke('getBrandWCPayRequest', {
                "appId": data.obj.appId,                  //公众号名称，由商户传入
                "timeStamp": data.obj.timeStamp,          //时间戳，自 1970 年以来的秒数
                "nonceStr": data.obj.nonceStr,         //随机串
                "package": data.obj.packageStr,      //商品包信息</span>
                "signType": data.obj.signType,        //微信签名方式
                "paySign": data.obj.paySign           //微信签名
            }, function (res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {// 支付成功
                    Feng.success("您已报名");
                    setTimeout(function(){
                        window.location.href="product_detil_succ.html?orderId="+orderId;
                    },1000);
                } else if (res.err_msg == "get_brand_wcpay_request:cancel") {//支付取消
                } else if (res.err_msg == "get_brand_wcpay_request:fail") {// 支付失败                    
                    Feng.success("支付错误，报名失败");
                }
            });
	    },function(data){
	    	console.log("微信支付失败");
	    	console.log(data);        	        
	        setTimeout(function(){
	        	window.location.href="my-order.html";
	        },1000);
	    });
	    ajax.type = "get";
	    ajax.start();
	}
};


//立即购买,保存订单
function buy(){
	var arry = sc;
	var courseData = JSON.stringify(arry);
	localStorage.setItem("courseData",courseData);
	//立即购买
	window.location.href="order.html";
}

$(function(){	
	product.detail();	
	
	product.initPay();
//	product.carLength();
});
